// 实现与MySQL交互
var mysql = require('mysql');
var config = require('../settings');
// 使用连接池，提升性能
var pool  = mysql.createPool(config.mysql);
var log = require('../logger.js');

module.exports = {
	
  //查询，自己写sql
  find:function(sql,cb){
  	 pool.getConnection(function(err, connection) {
		 //console.log(connection)
  	 	connection.query(sql,function(err, result) {
  	 		if(err){
  	 			log.logger.info(err);
  	 			cb(err);
  	 		}else{
  	 			cb(err,result);
  	 		}

  	 		connection.release();
  	 	});
  	 });
  },
  
  //根据id查询
  findById:function(param,cb){
  	pool.getConnection(function(err, connection) {
		//console.log(connection)
  		var sql="select * from "+param[0]+" where "+param[1]+" = ?";
  	 	connection.query(sql,[param[2]],function(err, result) {
  	 		if(err){
  	 			log.logger.info(err);
  	 			cb(err,[]);
  	 		}else{
  	 			if(result.length>0){
  	 				cb(err,result[0]);
  	 			}else{
  	 				cb(err,[]);
  	 			}
  	 		    
  	 		}
  	 		connection.release();
  	 	});
  	 });
  },
  
  //query  增，删，查，改
  querys:function(sql,params,cb){
  	pool.getConnection(function(err, connection) {
		console.log("进入query语句外围中");
		//console.log(connection)
  	 	connection.query(sql,params,function(err, result) {
  	 		if(err){
  	 			log.logger.info(err);
  	 			cb(err,{'datas':[]});
  	 		}else{
  	 		    cb(err,{'resultCode':'0','datas':result});
  	 		}
  	 		connection.release();
  	 	});
  	 });
  },



};